package com.xiaoyu.commonlib.utils;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 *
 * Created by jk on 2018/6/22.
 */
public class ViewPagerUtils {
    private  float buttonWidth;//每一个切换title控件的宽度
    private LinearLayout titleLayout;//title项容器
    private View mLine;//切换标识线
    private Context mContext;
    private String normalColor;//未选中文字颜色
    private String selectColor;//选中文字颜色
    private boolean isDivider;//切换项中间是否有分割线
    private int diviWid;//切换项中间分割线宽度（单位 dp）

    /**
     * @param mContext
     * @param titleLayout 切换项容器
     * @param mLine 下方滑动线
     */
    public ViewPagerUtils(Context mContext, LinearLayout titleLayout, View mLine) {
        this.titleLayout = titleLayout;
        this.mLine = mLine;
        this.mContext = mContext;
    }

    /**
     *
     * @param mContext
     * @param titleLayout
     * @param mLine
     * @param isDivider 切换项中间是否有分割线
     */
    public ViewPagerUtils(Context mContext, LinearLayout titleLayout, View mLine,boolean isDivider) {
        this.titleLayout = titleLayout;
        this.mLine = mLine;
        this.mContext = mContext;
        this.isDivider = isDivider;
    }

    public void initLine(){
        buttonWidth = UIUtils.getScreenWidth(mContext) / titleLayout.getChildCount();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        params.width = (int)buttonWidth;
        params.leftMargin = (int) ((buttonWidth - params.width) / 2);
        mLine.setLayoutParams(params);
    }

    public void initLine(int lineWidth){
        buttonWidth = UIUtils.getScreenWidth(mContext) / titleLayout.getChildCount();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        params.width = UIUtils.dip2px(lineWidth);
        params.leftMargin = (int) ((buttonWidth - params.width) / 2);
        mLine.setLayoutParams(params);
    }

    /**
     * 切换项中间有分割线的使用该方法
     * @param lineWidth
     * @param dividerWidth 切换项中间分割线的宽度
     */
    public void initLine(int lineWidth,int dividerWidth){
        int count = (titleLayout.getChildCount() + 1) / 2;
        int dividerNum = (titleLayout.getChildCount() - 1) / 2;
        diviWid = UIUtils.dip2px(dividerWidth);
        buttonWidth = (UIUtils.getScreenWidth(mContext) - diviWid * dividerNum ) / count;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        params.width = UIUtils.dip2px(lineWidth);
        params.leftMargin = (int) ((buttonWidth - params.width) / 2);
        mLine.setLayoutParams(params);
    }


    /**
     * 设置功能标志线位置
     * @param position
     */
    public void scrollLineMove(int position,float offset){
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        int leftMargin;
        if (isDivider){
            leftMargin = (int) ((buttonWidth - params.width) /2 + (buttonWidth + diviWid ) * (position + offset));
        }else {
            leftMargin = (int) ((buttonWidth - params.width) /2 + buttonWidth * (position + offset));
        }
        params.leftMargin = leftMargin;
        mLine.setLayoutParams(params);
    }

    /**
     * 设置功能titlebar选项的文字颜色
     * @param position
     */
    public void selectItem(int position){
        if (isDivider){
            for (int i = 0;i < titleLayout.getChildCount();i++){
                if (i %2 == 0){
                    TextView view = (TextView) titleLayout.getChildAt(i);
                    if (i == position *2){
                        view.setTextColor(Color.parseColor(selectColor));
                    }else {
                        view.setTextColor(Color.parseColor(normalColor));
                    }
                }

            }
        }else {
            for (int i = 0;i < titleLayout.getChildCount();i++){
                TextView view = (TextView) titleLayout.getChildAt(i);
                if (i == position){
                    view.setTextColor(Color.parseColor(selectColor));
                }else {
                    view.setTextColor(Color.parseColor(normalColor));
                }
            }
        }

    }

    /**
     * 设置文字颜色
     * @param normalColor 未选中文字颜色
     * @param selectColor 选中文字颜色
     */
    public void setTextColor(String normalColor,String selectColor){
        this.normalColor = normalColor;
        this.selectColor = selectColor;
    }
}
